import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

class Selectphotowidgets extends StatelessWidget {
  final int type;
  final Function callback;
  Selectphotowidgets(this.type, this.callback);
  List<AssetEntity> imgs = [];
  BuildContext contexts;
  final Imgpicker = ImagePicker();
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    contexts = context;
    return Container(
      child: Padding(
          padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
          child: Container(
            height: 80,
            child: new Column(children: <Widget>[
              new Container(
                  decoration: new BoxDecoration(
                      border: Border(
                          bottom:
                              BorderSide(width: 1, color: Color(0xffe5e5e5)))),
                  height: 40,
                  child: new GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () async {
                        Navigator.pop(context);

                        uploadImg(1);
                      },
                      child: Center(
                          child: Text('相机',
                              style: TextStyle(
                                  fontSize: 16, color: Colors.red))))),
              new Container(
                  height: 40,
                  child: new GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () async {
                        Navigator.pop(context);
                        imgs = await AssetPicker.pickAssets(context,
                            maxAssets: this.type == 1 ? 9 : 1);
                        uploadImgs();
                        // uploadImg(2);
                      },
                      child: Center(
                          child: Text('相册', style: TextStyle(fontSize: 16))))),
            ]),
          )),
    );
  }

  uploadImgs() async {
    if (imgs != null) {
      for (var item in imgs) {
        File file = await item.file;
        print(file.path);
        Map<String, String> map = new Map();
        HttpHelper.upLoadImage(File(file.path), (data) {
          // print('data');
          if (data is Object) {
            callback(data);
          } else {

          }
        }, params: map);
      }
    }
  }

  uploadImg(type) async {
    PickedFile selectimg;
    // File img;
    if (type == 1) {
      //相机
      selectimg = await Imgpicker.getImage(source: ImageSource.camera);
    } else {
      // List imgs = await PhotoPicker.pickAsset();
      selectimg = await Imgpicker.getImage(source: ImageSource.gallery);
    }
    if (selectimg == null) return;
    Map<String, String> map = new Map();
    HttpHelper.upLoadImage(File(selectimg.path), (data) {
      // print('data');
      if (data is Object) {
        callback(data);
      } else {}
    }, params: map);
  }
}
